/**
 * Copyright 2010 - YangJiandong(chunquedong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 
 * ChunMap是开源软件。你可以自由复制、传播本项目的下载包文件，但必须保持其完整性。
 * 我们不用对使用中的风险及由此造成的损失承担任何责任。
 * 详细情况请见《ChunMap许可协议》。
 * 
 * 想了解更多有关ChunMap的信息，请访问http://code.google.com/p/chunmap/
 * 
 * 下面此段程序作者YangJianDong(chunquedong)
 * 最后修改时间2010.7.17
 */
package chunmap.raster.gmap;

import java.awt.Image;
import java.io.IOException;

import chunmap.raster.Tile;
import chunmap.util.image.ImageCache;
import chunmap.util.image.WebClientUtil;

public class GMConnection {
	protected ImageCache cache=new ImageCache();
	public String rootUrl="http://mt2.google.cn/vt/v=w2.116&hl=zh-CN&gl=cn";

    public Image getImage(GMTile tile_)
    {
        Tile tile = (tile_.proxy == null) ? tile_ : tile_.proxy;

        String name = tile.toString();
        Image image = cache.find(name);
        if (image != null) return image;

        String url=getString(tile.x, tile.y, tile.z);
        try
        {
            image = request(url);
        }
        catch(IOException exp)
        {
            //return Resources.chun;
        	//TODO
        	return null;
        }

        cache.save(image, name);
        return image;
    }

    private String getString(int x, int y, int z)
    {
        String url=  rootUrl
        	+"&x="+ x + "&y=" + y + "&z=" + z + "&s=G";
        return url;
    }
    protected Image request(String url) throws IOException
    {
        return WebClientUtil.requestImage(url);
    }

    public void setBufferPath(String path)
    {
        cache.setBufferPath(path);
    }
}
